package cn.xinfei.xdecision.manager.controller.engine;

import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.enginex.po.Engine;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineCreateReqVo;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineListRespVo;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineUpdateReqVo;
import cn.xinfei.xdecision.common.model.login.SessionManager;
import cn.xinfei.xdecision.common.service.enginex.EngineService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 引擎管理
 */
@Api(tags = "引擎管理-引擎列表")
@RequestMapping("/v3/engine")
@RestController("engineControllerV3")
public class EngineController {

    @Resource
    private EngineService engineService;


    @PostMapping(value = "createEngine")
    @ApiOperation(value = "创建引擎",
            notes = "创建引擎")
    @ArchivesLog(type = OperateTypeEnum.CREATE, name = OperateNameConst.DecisionFlowOpt.CREATE_ENGINE)
    @ResponseBody
    public ResponseEntityDto<Boolean> createEngine(@RequestBody @Valid EngineCreateReqVo engineCreateReqVo) {
        return ResponseEntityBuilder.buildNormalResponse(engineService.createEngine(engineCreateReqVo));
    }

    @PostMapping(value = "updateEngine")
    @ApiOperation(value = "更新引擎信息",
            notes = "更新引擎信息")
    @ArchivesLog(type = OperateTypeEnum.CREATE, name = OperateNameConst.DecisionFlowOpt.UPDATE_ENGINE)
    @ResponseBody
    public ResponseEntityDto<Boolean> updateEngine(@RequestBody @Valid EngineUpdateReqVo engineUpdateReqVo) {
        return ResponseEntityBuilder.buildNormalResponse(engineService.updateEngine(SessionManager.getLoginAccount(), engineUpdateReqVo));
    }

    @GetMapping(value = "getEngineList")
    @ApiOperation(value = "获取引擎列表",
            notes = "查询引擎列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchString", value = "查询内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNo", value = "页码", defaultValue = "1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "10", dataTypeClass = Integer.class)
    })
    @ResponseBody
    public ResponseEntityDto<Map<String, Object>> enginIndex(@RequestParam(required = false) String searchString,
                                                             @RequestParam(defaultValue = "1") Integer pageNo,
                                                             @RequestParam(defaultValue = "10") Integer pageSize) {

        PageInfo<EngineListRespVo> engineListRespVos = engineService.getEngineList(searchString, pageNo, pageSize);
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("pager", engineListRespVos);
        hashMap.put("enginelist", engineListRespVos.getList());
        return ResponseEntityBuilder.buildNormalResponse(hashMap);
    }

    @GetMapping(value = "queryDetail/{code}")
    @ApiOperation(value = "查询引擎信息",
            notes = "查询引擎信息")
    @ResponseBody
    public ResponseEntityDto<Engine> queryDetail(@PathVariable String code) {
        Engine engineVo = engineService.getEngineByCode(code);
        return ResponseEntityBuilder.buildNormalResponse(engineVo);
    }


    @GetMapping(value = "getSonEngineList")
    @ApiOperation(value = "获取决策流子引擎列表")
    @ResponseBody
    public ResponseEntityDto<List<EngineListRespVo>> getSonEngineList(@RequestParam(value = "searchString", required = false) String searchCon,
                                                                      @RequestParam Long nodeId) {
        return ResponseEntityDto.success(engineService.getSonEngineList(searchCon, nodeId));
    }

}
